import React from 'react';
import styles from './index.less';
import { connect, Redirect } from 'umi'
import { UserModelState, LoginParams, ConnectProps, ConnectState } from '../../models/types'
import LoginForm from './LoginForm'
interface LoginProps extends ConnectProps {
  user: UserModelState
}
const LoginPage: React.FC<LoginProps> = ({ user, location, dispatch }) => {
  const { currentUser: { userid } } = user
  const isLogin = !!userid
  const handleSubmit = (value: LoginParams) => {
    dispatch({ type: 'user/login', payload: value })
  }
  if (isLogin) {
    const { from = '/' } = location.state || {}
    return <Redirect to={from}></Redirect>
  }
  return (
    <div className={styles.main}>
      <div className={styles.logo}></div>
      <LoginForm handleSubmit={handleSubmit} ></LoginForm>
    </div>
  );
}
export default connect(({ user }: ConnectState) => ({ user }))(LoginPage)